package com.fourth_groups.shopping121store.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fourth_groups.shopping121store.R;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.ButterKnife;
import butterknife.Unbinder;
/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃  创建人:李培雄
 * 　　┃　┳┛　┗┳　┃  时间:Administrator on 2016/10/10
 * 　　┃　　　　　　　┃  备注:主Activity
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━by:coder-pig
 */
public class MainActivity extends AppCompatActivity {

    RadioGroup mRg;
    private Unbinder bind;
    private Fragment HomeFragment;
    private Fragment BestFragment;
    private Fragment ShoppingBagFragment;
    private Fragment ClassifyFragment;
    private Fragment MyShiKuFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去标题
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        bind = ButterKnife.bind(this);


        init();
        initDao();

        Intent intent = getIntent();
        String flag = intent.getStringExtra("flag");
        if ("car".equals(flag)){
            select(3);
            RadioButton rB = (RadioButton) mRg.getChildAt(3);
            rB.setChecked(true);
        }
    }

    private void initDao() {
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.bt_home:
                        select(0);
                        break;
                    case R.id.bt_classify:
                        select(1);
                        break;
                    case R.id.bt_sight:
                        select(2);
                        break;
                    case R.id.bt_cart:
                        select(3);
                        break;
                    case R.id.bt_person:
                        select(4);
                        break;
                }
            }
        });
    }

    private void init() {
        //初始化第一个界面
        select(0);
        mRg = (RadioGroup) findViewById(R.id.main_sy);
    }

    private void select(int i) {
        FragmentManager manager = getSupportFragmentManager();//获得Fragment管理器
        FragmentTransaction ft = manager.beginTransaction();
        //开启一个事务
        hideFragment(ft);   //先隐藏 Fragment

        switch (i) {
            case 0:
                if (HomeFragment == null) {
                    HomeFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, HomeFragment);
                } else {
                    ft.show(HomeFragment);
                }
                break;
            case 1:
                if (BestFragment == null) {
                    BestFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, BestFragment);
                } else {
                    ft.show(BestFragment);
                }
                break;
            case 2:
                if (ClassifyFragment == null) {
                    ClassifyFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, ClassifyFragment);
                } else {
                    ft.show(ClassifyFragment);
                }
                break;
            case 3:
                if (ShoppingBagFragment == null) {
                    ShoppingBagFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, ShoppingBagFragment);
                } else {
                    ft.show(ShoppingBagFragment);
                }
                break;
            case 4:
                if (MyShiKuFragment == null) {
                    MyShiKuFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, MyShiKuFragment);
                } else {
                    ft.show(MyShiKuFragment);
                }
                break;
        }
        ft.commit();   //提交事务
    }

    //隐藏所有Fragment
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (HomeFragment != null) {
            fragmentTransaction.hide(HomeFragment);
        }
        if (BestFragment != null) {
            fragmentTransaction.hide(BestFragment);
        }
        if (ClassifyFragment != null) {
            fragmentTransaction.hide(ClassifyFragment);
        }
        if (ShoppingBagFragment != null) {
            fragmentTransaction.hide(ShoppingBagFragment);
        }
        if (MyShiKuFragment != null) {
            fragmentTransaction.hide(MyShiKuFragment);
        }
    }

    @Override
    protected void onDestroy() {
        bind.unbind();
        super.onDestroy();
    }

    //处理3秒内单击两次返回键退出程序
    private static Boolean isQuit = false;
    private Timer timer = new Timer();
    @Override
    public void onBackPressed() {
        if(isQuit){
            finish();
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        }else {
            isQuit = true;
//            Toast.makeText(MainActivity.this, "再次单击返回键退出程序", Toast.LENGTH_SHORT).show();
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    isQuit = false;
                }
            };
            timer.schedule(task,3000);
        }
    }


//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//    }
}
